/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.panomedic.kernel.controllers;

import com.panomedic.Constants;
import com.panomedic.core.Photo;
import com.panomedic.core.Photos;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import com.panomedic.gui.FileChooser;
import com.panomedic.gui.Menu;
import com.panomedic.gui.OptionsDialog2;
import com.panomedic.kernel.Kernel;
import com.panomedic.kernel.VisualHandler;
import com.panomedic.log4j.LogCategories;
import com.panomedic.log4j.Logger;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;

/**
 *
 * @author Yare
 */
public class MenuListener implements ActionListener {
    
    static Logger logger = new Logger();
    
    private Menu menu;
    private Preferences prefs;
    
    public MenuListener(Menu menu) {
        prefs = Preferences.userRoot().node(Constants.defNodePath);
        this.menu = menu;
    }

    public void actionPerformed (ActionEvent e){
        
        if( e.getSource() == menu.getFileOpenFiles()){
                        
            //ThumbList plist;
            //DEBUG System.out.println(System.currentTimeMillis());
            
            FileChooser fc = new FileChooser();
            fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
           
            File files[] = fc.getFiles();
            if (files != null) {
                VisualHandler.getInstance().getFrame().setStatus("Loading images...");
                for (File file : files) {
                    Photos.addPhoto(file);
                }
                Photos.makeIns();
            }
            VisualHandler.getInstance().getFrame().setStatus("Ready.");
        }
        else if( e.getSource() == menu.getFileOpenDirectory()) {
            
            FileChooser fc = new FileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
           
            File files[] = fc.getFiles();
            if (files != null) {
                VisualHandler.getInstance().getFrame().setStatus("Loading images...");
                for (File file : files) {
                    Photos.addPhoto(file);
                }
                Photos.makeIns();
            }
            VisualHandler.getInstance().getFrame().setStatus("Ready.");
            
            
        }
        else if ( e.getSource() == menu.getFileShowResult()) {
            Photo photo = Photos.getResult();
            if (photo != null)
                VisualHandler.getInstance().getFrame().setPhotoView(photo);
            else
                VisualHandler.getInstance().getFrame().setStatus("No result made.");
            
        }
        else if( e.getSource() == menu.getFileSaveResult()) {
            Photos.saveResult();
        }
        else if (e.getSource() == menu.getFileRemove()) {
            JList list =  VisualHandler.getInstance().getFrame().getList();
            int index = list.getSelectedIndex();
            
            if (index < 0){
                VisualHandler.getInstance().getFrame().setStatus("No photo seleceted.");
                return;
            }
            
            Photos.removePhoto(index);
            logger.info(LogCategories.GUI, "Image (" + index + ") removed.");
            
            int size = Photos.getInstance().getSize();
            
            if (size == 0)
                VisualHandler.getInstance().getFrame().setPhotoView();
            else {
                if (index == size)
                    index--;
                list.setSelectedIndex(index);
                list.ensureIndexIsVisible(index);
            }
            // TODO should be optimalized (not making all the intersections again)
            Photos.makeIns();       
            
        }
        else if( e.getSource() == menu.getFileRemoveAll()) {
            Photos.getInstance().clear();
            logger.info(LogCategories.GUI, "All images removed.");
            
            VisualHandler.getInstance().getFrame().setPhotoView();
            
        }
        else if( e.getSource() == menu.getImageProcess()) {
            if (Photos.getInstance().isEmpty()) {
                JOptionPane.showMessageDialog((JFrame)VisualHandler.getInstance().getFrame(),
                                    "No photos loaded.",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                return;
            }
            System.err.println("method " + prefs.getInt(Constants.METHOD_CHOICE, Constants.METHOD_LUT));
            switch(prefs.getInt(Constants.METHOD_CHOICE, Constants.METHOD_LUT)){
                case(Constants.METHOD_LUT):           Photos.processLevelMapping();   break;
                case(Constants.METHOD_STRETCHING):    Photos.processHistCalibration();   break;
            }
            VisualHandler.getInstance().getFrame().setPhotoView(Photos.getResult());
            // not implemented yet
            
        }
        else if( e.getSource() == menu.getToolsPreferences()){
            OptionsDialog2 dialog = new OptionsDialog2((JFrame) VisualHandler.getInstance().getFrame(), "Preferences", true);
        }
        else if( e.getSource() == menu.getFileExit()){
            Kernel.getInstance().Terminate();
        }
        else if( e.getSource() == menu.getHelpAbout()){
            JOptionPane.showMessageDialog((JFrame)VisualHandler.getInstance().getFrame(), Constants.APPLICATION_NAME+" version "
                  + Constants.APPLICATION_VERSION_NUMBER + " - Adjust images for your panorama.\n"+
                  "Copyright 2009 Jaroslav Fibichr (fibichrj@centrum.cz)\n"
                  +"This is free software, licenced under the GNU General Public License.",
                  "About", JOptionPane.INFORMATION_MESSAGE);
        }
        
    }
}
